package com.pearl.jacksonspringboot.demo.ser;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/**
 * @author TD
 * @version 1.0
 * @date 2024/4/16
 */
public class Jackson2NullValueSerializer {

    /**
     *  集合 null
     */
    public static class NullArrayJsonSerializer extends StdSerializer<Object> {

        public final static NullArrayJsonSerializer instance = new NullArrayJsonSerializer(Object.class);

        protected NullArrayJsonSerializer() {
            super(Object.class);
        }

        protected NullArrayJsonSerializer(Class<Object> t) {
            super(t);
        }

        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeStartArray(); // [
            jsonGenerator.writeEndArray(); // ]
        }
    }

    /**
     * 字符串 null
     */
    public static class NullStringJsonSerializer extends StdSerializer<Object> {

        public final static NullStringJsonSerializer instance = new NullStringJsonSerializer(Object.class);

        protected NullStringJsonSerializer() {
            super(Object.class);
        }


        protected NullStringJsonSerializer(Class<Object> t) {
            super(t);
        }

        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(""); // ""
        }
    }
}

